#include "pch.h"
#include "DXTools.h"
#include <ppltasks.h>

using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Microsoft::WRL;
using namespace Windows::Storage::Pickers;
using namespace Windows::Storage;
using namespace concurrency;
using namespace Platform;
using namespace Windows::Graphics::Display;
bool DXTools::CompareColor(ColorF lColor,UINT32 rColor)
{
	return false;
}

void DXTools::CreateBitmapFromFile(StorageFile^ file,
								   _In_ ComPtr<IWICImagingFactory2> wicFactory,
								   _In_ ComPtr<ID2D1DeviceContext> d2dDeviceContext,
								   _Out_  ID2D1Bitmap** d2d1Bitmap)
{
	ComPtr<IWICBitmapDecoder> decoder;
	ComPtr<IWICBitmapFrameDecode> frame;
	ComPtr<IWICFormatConverter> wicBitmap;
	ID2D1Bitmap* bmp;
	IWICStream* wicStream;
	wicFactory->CreateStream(&wicStream);

	DX::ThrowIfFailed(
		wicFactory->CreateDecoderFromFilename(L"1.jpg",
		nullptr,
		GENERIC_READ,
		WICDecodeMetadataCacheOnDemand,
		&decoder)
		);

	DX::ThrowIfFailed(
		decoder->GetFrame(0, &frame)
		);

	DX::ThrowIfFailed(
		wicFactory->CreateFormatConverter(&wicBitmap)
		);

	// I'm not sure I use the correct parameters, they are taken from a code sample.
	DX::ThrowIfFailed(
		wicBitmap->Initialize(
		frame.Get(),
		GUID_WICPixelFormat32bppPBGRA,
		WICBitmapDitherTypeNone,
		nullptr,
		0,
		WICBitmapPaletteTypeCustom));

	// This is the call which returns the OutOfMemory error for large images.
	auto hr = d2dDeviceContext->CreateBitmapFromWicBitmap(
		wicBitmap.Get(),
		D2D1::BitmapProperties(
		D2D1::PixelFormat(),
		DisplayProperties::LogicalDpi,
		DisplayProperties::LogicalDpi),
		&bmp);
	*d2d1Bitmap=bmp;
}